import numpy as np
from PIL import Image,ImageOps
import colorsys

im = np.loadtxt("Grid/FloodFill/flood.asc", skiprows=6)
im_o = np.loadtxt("Grid/FloodFill/terrain.asc", skiprows=6)
for i,line in enumerate(im):
    for j,col in enumerate(line):
        if col != 0:
            im_o[i][j] = -20




# 将叠加后的数据显示出来
im = Image.fromarray(im_o).convert("L")

# 图片增强
im = ImageOps.equalize(im)
im = ImageOps.autocontrast(im)

# 开始构造渐变颜色调色板
palette = []

# 色调，饱和度，值
# 色彩空间始于黄色
h = .67  # 色调
s = 1    # 饱和度
v = 1    # 值

# 渐变颜色依次为 蓝-绿-黄-橙-红；蓝=底高程区域 红=高程较高区域
step = h/256.0
# 构建调色板
for i in range(256):
    rp, gp, bp = colorsys.hsv_to_rgb(h, s, v)
    r = int(rp * 255)
    g = int(gp * 255)
    b = int(bp * 255)
    palette.extend([r, g, b])
    h -= step

# 将调色板应用到图片上
im.putpalette(palette)

# 保存图片
im.save("Grid/FloodFill/result.bmp")

